package com.easy.yours.biz.util;

import com.easy.system.util.Condition;
import org.hibernate.type.*;


import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: sunguangzhu
 * Date: 13-7-1
 * Time: 下午4:29
 * To change this template use File | Settings | File Templates.
 */
public class SqlUtil {
    public static Map<String, Object> formatSql(StringBuilder stringBuilder, Condition condition) {
        Map<String, Object> map = SqlUtil.appendCondtions(stringBuilder, condition);
        return map;
    }

    private static Map<String, Object> appendCondtions(StringBuilder stringBuilder, Condition condition) {
        Map map = new HashMap();
        Object[] values = null;
        Type[] types = null;
        if (condition != null) {
            String alias = "";
            String blank = " ";
            if (condition.getAlias() != null) {
                alias = condition.getAlias();
            } else {
                alias = "obj";
            }
            if (condition.getChildren() != null) {
                values = new Object[condition.getChildren().size()];
                types = new Type[condition.getChildren().size()];

                for (int i = 0; i < condition.getChildren().size(); i++) {
                    Condition conditionChildren = condition.getChildren().get(i);
                    stringBuilder.append(blank + conditionChildren.getRelation() + blank + alias + "." + conditionChildren.getProperty() + blank + conditionChildren.getOperate() + blank + "?");
                    values[i] = conditionChildren.getValue();
                    types[i] = SqlUtil.getType(conditionChildren.getValue());
                }
            }
        }
        map.put("hql", stringBuilder);
        map.put("values", values);
        map.put("types", types);
        return map;
    }


    public static Type getType(Object value) {
        Type type = null;
        if (value.getClass().getName().endsWith("String")) {
            type = new StringType();
        } else if (value.getClass().getName().endsWith("Integer")) {
            type = new IntegerType();
        } else if (value.getClass().getName().endsWith("Float")) {
            type = new FloatType();
        } else if (value.getClass().getName().endsWith("Date")) {
            type = new DateType();
        } else if (value.getClass().getName().endsWith("Long")) {
            type = new LongType();
        }
        return type;
    }


    public static StringBuilder getSelectSql(StringBuilder beforeFromStringBuilder, Condition condition) {
        String alias = "";
        String blank = " ";
        StringBuilder stringBuilder = new StringBuilder();
        if (beforeFromStringBuilder != null) {
            stringBuilder.append(beforeFromStringBuilder.toString() + blank);
        }
        if (condition != null) {
            if (condition.getAlias() != null) {
                alias = condition.getAlias();
            } else {
                alias = "obj";
            }
            stringBuilder.append(" from " + condition.getClassName() + blank + alias);
            if (condition.getChildren() != null && condition.getChildren().size() > 0) {
                if (condition.getChildren().get(0).getRelation().equals("or")) {
                    stringBuilder.append(" where 1 = 2 ");
                } else {
                    stringBuilder.append(" where 1 = 1 ");
                }
            } else {
                stringBuilder.append(" where 1 = 1 ");
            }
        }
        return stringBuilder;
    }

    public static Object[] getValues(Condition condition) {
        return null;
    }

    public static String getParams(Condition condition) {
//        String  sql = " from "+condition.getClassName()+" where 1 = 1 ";
        return null;
    }
}
